package com.ruoyi.web.controller.substation;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.domain.SProtectDevice;
import com.ruoyi.system.domain.SProtectDeviceVersion;
import com.ruoyi.system.domain.SSubstation;
import com.ruoyi.system.domain.vo.SSubstationDeviceVo;
import com.ruoyi.system.service.ISProtectDeviceService;
import com.ruoyi.system.service.ISSubstationService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SSubstationDevice;
import com.ruoyi.system.service.ISSubstationDeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 变电站设备信息Controller
 * 
 * @author ruoyi
 * @date 2023-07-05
 */
@Api(tags = "变电站设备信息管理")
@RestController
@RequestMapping("/substation/substationDevice")
public class SSubstationDeviceController extends BaseController
{
    @Autowired
    private ISSubstationDeviceService sSubstationDeviceService;
    @Autowired
    private ISProtectDeviceService protectDeviceService;
    @Autowired
    private ISSubstationService substationService;

    /**
     * 查询变电站设备信息列表
     */
    @ApiOperation("获取变电站设备信息列表")
    @PreAuthorize("@ss.hasPermi('substation:substationDevice:list')")
    @GetMapping("/list")
    public TableDataInfo list(SSubstationDevice sSubstationDevice)
    {
        startPage();
        List<SSubstationDevice> list = sSubstationDeviceService.getList(sSubstationDevice);
        return getDataTable(list);
    }

    /**
     * 查询变电站设备详细信息列表
     */
    @ApiOperation("查询变电站设备详细信息（包含保护装置的三个版本）列表")
    @PreAuthorize("@ss.hasPermi('substation:substationDevice:list')")
    @GetMapping("/detailList")
    public TableDataInfo detailList(SSubstationDevice sSubstationDevice)
    {
        startPage();
        List<Map<String, Object>> list = sSubstationDeviceService.detailList(sSubstationDevice);
        return getDataTable(list);
    }

    /**
     * 获取变电站设备名称列表
     */
    @ApiOperation("获取变电站设备名称列表")
    @PreAuthorize("@ss.hasPermi('substation:substationDevice:list')")
    @GetMapping("/deviceNameList")
    public AjaxResult deviceNameList(SSubstationDevice sSubstationDevice)
    {
        Map<String, String> map = sSubstationDeviceService.deviceNameList(sSubstationDevice);
        return success(map);
    }

    /**
     * 导出变电站设备信息列表
     */
    @ApiOperation("导出变电站设备信息列表")
    @PreAuthorize("@ss.hasPermi('substation:substationDevice:export')")
    @Log(title = "变电站设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SSubstationDevice sSubstationDevice)
    {
        List<SSubstationDevice> list = sSubstationDeviceService.getList(sSubstationDevice);
        ExcelUtil<SSubstationDevice> util = new ExcelUtil<SSubstationDevice>(SSubstationDevice.class);
        util.exportExcel(response, list, "变电站设备信息数据");
    }


    /**
     * 导入变电站设备信息
     */
    @ApiOperation("导入变电站设备信息")
    @PreAuthorize("@ss.hasPermi('substation:substationDevice:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        String message = "操作成功";
        try {
            message = sSubstationDeviceService.importData(file);
        }catch (IllegalStateException e){
            throw new ServiceException("请检查表格格式是否为文本格式!");
        }

        return success(message);
    }


    /**
     * 获取变电站设备信息详细信息
     */
    @ApiOperation("获取变电站设备信息详细")
    @ApiImplicitParam(name = "id", value = "变电站设备信息ID", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('substation:substationDevice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        SSubstationDevice substationDevice = sSubstationDeviceService.getById(id);
        if(StringUtils.isNotNull(substationDevice)) {
            // 保护装置型号
            String protectDeviceModel = protectDeviceService.getProtectDeviceModelByDeviceId(id);
            substationDevice.setProtectDeviceModel(protectDeviceModel);
            // 所属变电站
            SSubstation substation = substationService.getById(substationDevice.getSubstationId());
            if(StringUtils.isNotNull(substation)) {
                substationDevice.setSubstationName(substation.getSubstationName());
            }
        }
        return success(substationDevice);
    }

    /**
     * 新增变电站设备信息
     */
    @ApiOperation("新增变电站设备信息")
    @PreAuthorize("@ss.hasPermi('substation:substationDevice:add')")
    @Log(title = "变电站设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SSubstationDevice sSubstationDevice)
    {
        // 校验名称
        sSubstationDeviceService.checkSubstationDevice(sSubstationDevice);
        boolean save = sSubstationDeviceService.save(sSubstationDevice);
        // 生成二维码
        sSubstationDeviceService.createQRCode(sSubstationDevice.getId());
        return toAjax(save);
    }

    /**
     * 修改变电站设备信息
     */
    @ApiOperation("更新变电站设备信息")
    @PreAuthorize("@ss.hasPermi('substation:substationDevice:edit')")
    @Log(title = "变电站设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SSubstationDevice sSubstationDevice)
    {
        // 校验名称
        SSubstationDevice byId = sSubstationDeviceService.getById(sSubstationDevice.getId());
        if(!sSubstationDevice.getDeviceName().equals(byId.getDeviceName())) {
            sSubstationDeviceService.checkSubstationDevice(sSubstationDevice);
        }
        boolean b = sSubstationDeviceService.updateById(sSubstationDevice);
        // 生成二维码
        sSubstationDeviceService.createQRCode(sSubstationDevice.getId());
        return toAjax(b);
    }

    /**
     * 扫描设备二维码
     */
    @ApiOperation("扫描设备二维码")
    @PreAuthorize("@ss.hasPermi('substation:substationDevice:list')")
    @PostMapping("/scanQrCode")
    public AjaxResult scanQrCode(@RequestBody SSubstationDeviceVo vo)
    {
        Map<String, Object> map = sSubstationDeviceService.scanQrCode(vo);
        return success(map);
    }

    /**
     * 删除变电站设备信息
     */
    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "ids", value = "变电站设备信息ID集合", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('substation:substationDevice:remove')")
    @Log(title = "变电站设备信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(sSubstationDeviceService.removeSubstationDeviceByIds(ids));
    }


    /**
     * 获取变电站设备的保护装置列表
     */
    @ApiOperation("获取变电站设备的保护装置列表")
    @PreAuthorize("@ss.hasPermi('substation:substationDevice:list')")
    @GetMapping("/protectDeviceList")
    public AjaxResult protectDeviceList(SSubstationDevice sSubstationDevice)
    {
        List<SProtectDevice> list = sSubstationDeviceService.getProtectDeviceList(sSubstationDevice.getId());
        return success(list);
    }

}
